package dk.itu.spct;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class ImageClient implements Runnable {
	
	int serverPort = 45678;
	File myFile;
	
	public ImageClient(File myFile) {
		this.myFile = myFile;
	}
	
	@Override
	public void run() {
		InetAddress serverAddress;
		try {
			serverAddress = InetAddress.getByName("10.27.249.193"); //10.27.249.193
			Socket socket = new Socket(serverAddress, serverPort);
			
			//myFile = new File ("C:\\Users\\Jesper\\workspace\\mt4j-desktop\\examples\\images\\Jesper Sandberg.jpg");
		    byte [] mybytearray  = new byte [(int)myFile.length()];
		    FileInputStream fis = new FileInputStream(myFile);
		    BufferedInputStream bis = new BufferedInputStream(fis);
		    bis.read(mybytearray,0,mybytearray.length);
		    
		    OutputStream os = socket.getOutputStream();
		    DataOutputStream dos = new DataOutputStream(os);
		    
	        dos.writeInt((int)myFile.length());
	        dos.write(mybytearray,0,mybytearray.length);
	        
	        System.out.println("Size: "+mybytearray.length);
	        
	        dos.flush();
	        dos.close();
	        os.close();
	        bis.close();
	        fis.close();
	        socket.close();
	        
	        try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
